package com.thb.pay.payment.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.thb.pay.api.payment.PayPassageAccountServiceApi;
import com.thb.pay.api.payment.vo.req.PayPassageAccountPageReq;
import com.thb.pay.api.payment.vo.req.PayPassageAccountReq;
import com.thb.pay.api.payment.vo.resp.PayPassageAccountVo;
import com.thb.pay.core.exception.RRException;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.entity.SuperEntity;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.payment.entity.PayPassageAccount;
import com.thb.pay.payment.service.PayPassageAccountService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.collection.CollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 支付-通道-账户表 前端控制器
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@RestController
public class PayPassageAccountController implements PayPassageAccountServiceApi {

    @Autowired
    private PayPassageAccountService payPassageAccountService;

    @Override
    public PageResult<PayPassageAccountVo> findPage(@RequestBody PayPassageAccountPageReq pageReq) {
        IPage<PayPassageAccount> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<PayPassageAccount> pageResult = payPassageAccountService.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, PayPassageAccountVo.class);
    }

    @Override
    public List<PayPassageAccountVo> findAllList() {
        List<PayPassageAccountVo> resultDataList = Lists.newArrayList();
        List<PayPassageAccount> targetDbResultList = payPassageAccountService.selectList(null);
        targetDbResultList.forEach(dbResult -> {
            PayPassageAccountVo resultData = new PayPassageAccountVo();
            BeanUtil.copyProperties(dbResult, resultData);
            resultDataList.add(resultData);
        });
        return resultDataList;
    }

    @Override
    public List<PayPassageAccountVo> findListByPassageId(@RequestParam("payPassageId") Long payPassageId) {
        List<PayPassageAccountVo> resultDataList = Lists.newArrayList();
        LambdaQueryWrapper<PayPassageAccount> wrapper = new LambdaQueryWrapper();
        wrapper.eq(PayPassageAccount::getPayPassageId, payPassageId);
        List<PayPassageAccount> targetDbResultList = payPassageAccountService.selectList(wrapper);
        targetDbResultList.forEach(dbResult -> {
            PayPassageAccountVo resultData = new PayPassageAccountVo();
            BeanUtil.copyProperties(dbResult, resultData);
            resultDataList.add(resultData);
        });
        return resultDataList;
    }

    @Override
    public void add(@RequestBody PayPassageAccountReq payPassageAccountReq) {

        LambdaQueryWrapper<PayPassageAccount> wrapper = new LambdaQueryWrapper();
        wrapper.eq(PayPassageAccount::getPayPassageId, payPassageAccountReq.getPayPassageId());
        wrapper.eq(PayPassageAccount::getPassageMchId, payPassageAccountReq.getPassageMchId());
        List<PayPassageAccount> targetDbResultList = payPassageAccountService.selectList(wrapper);
        if (CollUtil.isNotEmpty(targetDbResultList)) {
            throw RRException.build("passage.account.exist");
        }
        PayPassageAccount payPassageAccount = new PayPassageAccount();
        BeanUtil.copyProperties(payPassageAccountReq, payPassageAccount);
        payPassageAccountService.insert(payPassageAccount);
    }

    @Override
    public void update(@RequestBody PayPassageAccountReq payPassageAccountReq) {
        PayPassageAccount payPassageAccount = payPassageAccountService.getById(payPassageAccountReq.getId());
        if (payPassageAccount == null) {
            throw RRException.build("passage.account.not.exist");
        }

        LambdaQueryWrapper<PayPassageAccount> wrapper = new LambdaQueryWrapper();
        wrapper.eq(PayPassageAccount::getPayPassageId, payPassageAccountReq.getPayPassageId());
        wrapper.eq(PayPassageAccount::getPassageMchId, payPassageAccountReq.getPassageMchId());
        List<PayPassageAccount> targetDbResultList = payPassageAccountService.selectList(wrapper);
        if (CollUtil.isNotEmpty(targetDbResultList)) {
            if (targetDbResultList.get(0).getId().compareTo(payPassageAccountReq.getId()) != 0) {
                throw RRException.build("passage.account.exist");
            }
        }
        PayPassageAccount targetPayPassageAccount = new PayPassageAccount();
        BeanUtil.copyProperties(payPassageAccountReq, targetPayPassageAccount);
        payPassageAccountService.updateById(targetPayPassageAccount);
    }

    @Override
    public PayPassageAccountVo getById(@RequestParam("id") Long id) {
        PayPassageAccount payPassageAccount = payPassageAccountService.selectById(id);
        if (payPassageAccount != null) {
            PayPassageAccountVo resultData = new PayPassageAccountVo();
            BeanUtil.copyProperties(payPassageAccount, resultData);
            return resultData;
        }
        return null;
    }

    @Override
    public void deleteBatchIds(@RequestBody List<Long> ids) {
        LambdaQueryWrapper<PayPassageAccount> wrapper = new LambdaQueryWrapper();
        wrapper.in(PayPassageAccount::getId, ids);
        List<PayPassageAccount> targetDbResultList = payPassageAccountService.selectList(wrapper);
        if (CollUtil.isNotEmpty(targetDbResultList)) {
            targetDbResultList.forEach(payType -> {
                payType.setDelFlag(SuperEntity.DEFAULT_DELETE_FLAG);
            });
            payPassageAccountService.updateBatchById(targetDbResultList);
        }
    }
}
